package com.oscarmovie.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.struts2.json.annotations.JSON;

import tt.util.TimeUtils;

import com.oscarmovie.Tools;

@SuppressWarnings("serial")
public class Movie implements Serializable {
	
	private int id;
	private String name;
	private String nameCn;
	private String alias;
	private String plot;
	private Date datetime;
	private int length;
	private int doubanId;
	private int mtimeId;
	
	private Image image;
	private Calendar calendar;
	private Set<Mpr> mprs = new HashSet<Mpr>();
	//many-to-many
	private Set<Category> categories;
	private Set<Country> countries = new HashSet<Country>();
	//one-to-many
	private Set<Music> musics = new HashSet<Music>();
	//one-to-many
	private Set<ImageMovie> images = new HashSet<ImageMovie>();
	//many-to-many
	private Set<Language> languages;
	private Set<Video> videos = new TreeSet<Video>();
	private Set<Mpac> mpacs = new HashSet<Mpac>();
	private Set<Datetime> datetimes = new HashSet<Datetime>();
	private String displayName;
	private Statistics statistics;
	
	private Set<CommentMovie> comments;
	private Set<DescriptionMovie> plots;

	public String getDisplayName() {
		if (displayName == null) {
			displayName = name;
			
			if (nameCn!=null && !"".equals(nameCn)) {
				displayName = nameCn ;
			}
		}
		return displayName;
	}
	
	/**
	 * @param displayName the displayName to set
	 */
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getNameCn() {
		return nameCn;
	}
	public void setNameCn(String nameCn) {
		this.nameCn = nameCn;
	}
	public String getAlias() {
		return alias;
	}
	public void setAlias(String alias) {
		this.alias = alias;
	}
	public String getPlot() {
		return plot;
	}
	public void setPlot(String plot) {
		this.plot = plot;
	}
	public void setMprs(Set<Mpr> mprs) {
		this.mprs = mprs;
	}
	public Set<Mpr> getMprs() {
		return mprs;
	}
	public void setCategories(Set<Category> categories) {
		this.categories = categories;
	}
	public Set<Category> getCategories() {
		return categories;
	}
	public void setCountries(Set<Country> countries) {
		this.countries = countries;
	}
	public Set<Country> getCountries() {
		return countries;
	}
	public void setMusics(Set<Music> musics) {
		this.musics = musics;
	}
	public Set<Music> getMusics() {
		return musics;
	}
	public void setImages(Set<ImageMovie> images) {
		this.images = images;
	}
	public Set<ImageMovie> getImages() {
		return images;
	}
	public void setImage(Image image) {
		this.image = image;
	}
	public Image getImage() {
		return image;
	}
	public void setVideos(Set<Video> videos) {
		this.videos = videos;
	}
	
	public Set<Video> getVideos() {
		Set<Video> ss = new TreeSet<Video>();
		ss.addAll(videos);
		return ss;
	}
	public void setLanguages(Set<Language> languages) {
		this.languages = languages;
	}
	public Set<Language> getLanguages() {
		return languages;
	}
	
	public void setMpacs(Set<Mpac> mpacs) {
		this.mpacs = mpacs;
	}
	public Set<Mpac> getMpacs() {
		return mpacs;
	}
	
	public List<Mpac> getSortedMpacs() {
		List<Mpac> sortedMpacs = new ArrayList();
		for (Mpac mpac : getMpacs()) {
			sortedMpacs.add(mpac);
		}
		
		Collections.sort(sortedMpacs, new Comparator<Mpac>(){

			
			public int compare(Mpac o1, Mpac o2) {
				if (o1.getPerson() == null && o2.getPerson() != null) {
					return -1;					
				} else if (o1.getPerson() != null && o2.getPerson() == null) {
					return 1;
				} else if (o1.getPerson() != null && o2.getPerson() != null){
					if (o1.getType() == 2 && o2.getType() ==1) {
						return -1;
					} else if (o1.getType() == 1 && o2.getType() ==2) {
						return 1;
					}
				}			
								
				return 0;
			}
		});
		return sortedMpacs;
	}
	
	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}
	public Calendar getCalendar() {
		return calendar;
	}
	public void setDatetimes(Set<Datetime> datetimes) {
		this.datetimes = datetimes;
	}
	public Set<Datetime> getDatetimes() {
		return datetimes;
	}
	public void setDatetime(Date datetime) {
		this.datetime = datetime;
	}
	
	@JSON(format="yyyy-MM-dd")
	public Date getDatetime() {
		return datetime;
	}
	public String getDisplayDatetime(){
		return TimeUtils.getOnlyDate(datetime);
	}
	public int getDatetimeYear(){
		return Integer.parseInt(Tools.getOnlyYear(datetime));
	}
	public String getYear() {
		return Tools.getOnlyYear(datetime);
	}
	public void setLength(int length) {
		this.length = length;
	}
	public int getLength() {
		return length;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	
	public boolean equals(Object present) {
		if (this == present)
			return true;
		if (present == null) {
			return false;
		}
		
		if (!getClass().isAssignableFrom( present.getClass())){
			return false;
		}
		
		Movie other = (Movie) present;
		if (getId() != other.getId()){
			return false;
		}
			
		return true;
	}

	public void setMtimeId(int mtimeId) {
		this.mtimeId = mtimeId;
	}

	public int getMtimeId() {
		return mtimeId;
	}

	public void setDoubanId(int doubanId) {
		this.doubanId = doubanId;
	}

	public int getDoubanId() {
		return doubanId;
	}

	public void setStatistics(Statistics statistics) {
		this.statistics = statistics;
	}

	public Statistics getStatistics() {
		//TODO created a problem when adding a new movie via console.
//		if (statistics == null) {
//			statistics = new Statistics();
//		}
		return statistics;
	}

	public void setComments(Set<CommentMovie> comments) {
		this.comments = comments;
	}

	public Set<CommentMovie> getComments() {
		return comments;
	}

	public void setPlots(Set<DescriptionMovie> plots) {
		this.plots = plots;
	}

	public Set<DescriptionMovie> getPlots() {
		return plots;
	}
	
	
}
